package com.qimingyu.recursion;

/**
 * @version 1.0
 * @Author doreaxon
 * @Date 2023/8/29 21:26
 * @注释 递归实现二分查找
 */
public class BinarySearchWithRecursion {

    public static int binarySearch(int[] arr, int left, int right, int target) {
        if (left > right) {
            return -1;
        }

        int middle = (left + right) >>> 1;
        if (arr[middle] < target) {
            left = middle + 1;
            return binarySearch(arr, left, right, target);
        } else if (target < arr[middle]) {
            right = middle - 1;
            return binarySearch(arr, left, right, target);
        } else {
            return middle;
        }

    }

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        System.out.println(binarySearch(arr, 0, 4, 4));
    }
}
